import vue from "vue"
import VueRouter from "vue-router"

vue.use(VueRouter)
import { getToken } from '@/utils/auto'
import user from '@/router/modules/users'
import product from '@/router/modules/products'
import category from '@/router/modules/category'
// 框架内
const fadeIn = [
    {
        path: '/admin',
        component: () => import('../view/Main/Home'),
        children: [
            {
                path: '/',
                redirect: "welcome"
            },
            {
                path: 'welcome',
                component: () => import('../view/Main/Welcome'),
            },
            user,
            product,
            category
        ]
    }
]

// 框架外
const fadeOut = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../view/Account/Login')
    },
]

const router = new VueRouter({
    routes: [
        ...fadeOut,
        ...fadeIn,
    ]
})

router.beforeEach((to, from, next) => {
    // 当访问的非登陆页面且未登录就阻止（参考文档）
    if (to.name !== 'Login' && !getToken()) next({ name: 'Login' })
    else next()
})


export default router

